<?php
class Default_Model_DbTable_News extends Zend_Db_Table_Abstract {
	protected $_name = 'news';
	public function addNews($category_id, $admin_id, $title, $update, $short_content, $long_content, $tag, $img, $status) {
		$data = array (
				'CATEGORY_ID' => $category_id,
				'ADMIN_ID' => $admin_id,
				'TITLE' => $title,
				'UPDATE' => $update,
				'SHORT_CONTENT' => $short_content,
				'LONG_CONTENT' => $long_content,
				"IMG" => $img,
				"TAGS" => $tag,
				"VIEW" => 0,
				'STATUS' => $status 
		);
		try {
			return $this->insert ( $data );
		} catch ( Exception $e ) {
			print_r ( $e );
			die ();
		}
		return null;
	}
	public function updateStatus($id, $status) {
		$data = array (
				'STATUS' => $status 
		);
		return $this->update ( $data, "ID = $id" );
	}
	public function updateNews($id, $category_id, $admin_id, $title, $update, $short_content, $long_content, $tag, $img, $status) {
		$data = array (
				'CATEGORY_ID' => $category_id,
				'ADMIN_ID' => $admin_id,
				'TITLE' => $title,
				'UPDATE' => $update,
				'SHORT_CONTENT' => $short_content,
				'LONG_CONTENT' => $long_content,
				"IMG" => $img,
				"TAGS" => $tag,
				"VIEW" => 0,
				'STATUS' => $status 
		);
		return $this->update ( $data, "ID=$id" );
	}
	public function removeNews($id) {
		return $this->delete ( "ID = $id" );
	}
	public function getAllNews() {
		return $this->fetchAll ( "STATUS = 1 AND CATEGORY_ID != 27 AND CATEGORY_ID != 28 AND CATEGORY_ID != 29 AND CATEGORY_ID != 30 AND CATEGORY_ID != 31  AND CATEGORY_ID != 32 AND CATEGORY_ID != 33 AND CATEGORY_ID != 34 AND CATEGORY_ID != 35 AND CATEGORY_ID != 24 AND CATEGORY_ID != 25 AND CATEGORY_ID != 26 AND CATEGORY_ID != 12 AND CATEGORY_ID != 13  AND CATEGORY_ID != 23 AND CATEGORY_ID != 22 AND CATEGORY_ID != 21 AND CATEGORY_ID != 19 AND CATEGORY_ID != 20 AND CATEGORY_ID != 14 AND CATEGORY_ID != 15 AND CATEGORY_ID != 16 AND CATEGORY_ID != 17 AND CATEGORY_ID != 18", "ID DESC" )->toArray ();
	}
	public function getAllNewsAdmin() {
		return $this->fetchAll ( "STATUS = 1", "ID DESC" )->toArray ();
	}
	public function getNewsById($id) {
		return $this->fetchRow ( "id = $id" );
	}
	public function getNewsById1($id, $cate_id) {
		return $this->fetchRow ( "id = $id AND CATEGORY_ID = $cate_id" )->toArray ();
	}
	public function getNewsByName($title) {
		return $this->fetchRow ( "TITLE = $title" );
	}
	public function getNewsByAdminId($admin_id) {
	}
	public function getNewsByAdminName($admin_name) {
		$table = new Admin_Model_DbTable_Admin ();
		$admin = $table->getAdminByName ( $admin_name, 1 );
		$id = $admin ["ID"];
		$select = $this->select ( Zend_Db_Table::SELECT_WITH_FROM_PART )->setIntegrityCheck ( false );
		$select->from ( "news as n" )->joinLeft ( "admin as ad", "n.ADMIN_ID = ad.ID" )->where ( "n.ADMIN_ID = $id" );
		return $news = $this->fetchAll ( $select );
	}
	public function getNewsByCategoryId($category_id, $newsId) {
		// Trước khi muốn chạy join phải thêm dòng thiết lập
		// Select_with_from_part và setIntegritycheck là false
		$select = $this->select ( Zend_Db_Table::SELECT_WITH_FROM_PART )->setIntegrityCheck ( false );
		if ($newsId != null) {
			$select->from ( "news as n" )->joinLeft ( "category as ca", "n.CATEGORY_ID = ca.ID" )->where ( "n.CATEGORY_ID = $category_id AND n.ID != $newsId" )->order ( "n.ID DESC" )->group ( "n.id" )->limit ( 5, 0 );
		} else {
			$select->from ( "news as n" )->joinLeft ( "category as ca", "n.CATEGORY_ID = ca.ID" )->where ( "n.CATEGORY_ID = $category_id AND n.STATUS = 1" )->order ( "n.ID DESC" )->group ( "n.id" )->limit ( 5, 0 );
		}
		return $News = $this->fetchAll ( $select );
	}
	public function getNewsByCategoryId1($category_id, $newsId) {
		// Trước khi muốn chạy join phải thêm dòng thiết lập
		// Select_with_from_part và setIntegritycheck là false
		$select = $this->select ( Zend_Db_Table::SELECT_WITH_FROM_PART )->setIntegrityCheck ( false );
		$select->from ( "news as n" )->joinLeft ( "category as ca", "n.CATEGORY_ID = ca.ID" )->where ( "n.CATEGORY_ID = $category_id AND n.ID != $newsId" )->order ( "n.ID DESC" )->group ( "n.id" )->limit ( 5, 0 );
		return $News = $this->fetchAll ( "CATEGORY_ID = $category_id AND STATUS = 1 AND id != $newsId", "id DESC", 10 );
	}
	public function getNewsByCategoryIdAdmin($category_id) {
		$select = $this->select ( Zend_Db_Table::SELECT_WITH_FROM_PART )->setIntegrityCheck ( false );
		$select->from ( "news as n" )->joinLeft ( "category as ca", "n.CATEGORY_ID = ca.ID" )->where ( "n.CATEGORY_ID = $category_id AND n.STATUS = 1" )->order ( "n.ID DESC" )->group ( "n.id" );
		return $News = $this->fetchAll ( $select );
	}
	public function getNewsByCategoryName($category_name) {
		$table = new Default_Model_DbTable_Category ();
		$cat = $table->getCategoryByName ( $category_name );
		$catID = $cat ["ID"];
		return $this->fetchAll ( "CATEGORY_ID = $catID" );
	}
	public function getNewsByStatus($status) {
		return $this->fetchAll ( "STATUS = $status" )->toArray();
	}
	public function getNewsByStatus1($cate_id, $status) {
		return $this->fetchAll ( "CATEGORY_ID = $cate_id AND STATUS = $status" )->toArray();
	}
	public function addView($id) {
		$n = $this->fetchRow ( "ID = $id" );
		return $this->update ( array (
				"VIEW" => $n ["VIEW"] + 1 
		), "id = $id" );
	}
	public function updateStt($id) {
		$n = $this->fetchRow ( "ID = $id" );
		if ($n ["STATUS"] == 1) {
			$this->update ( array (
					"STATUS" => 0 
			), "ID = $id" );
		} else {
			$this->update ( array (
					"STATUS" => 1 
			), "ID = $id" );
		}
	}
	public function randomNews() {
		$select = $this->select ()->from ( "news", array (
				"id",
				"TITLE",
				"UPDATE" 
		) )->where ( "CATEGORY_ID != 27 AND CATEGORY_ID != 28 AND CATEGORY_ID != 29 AND CATEGORY_ID != 30 AND CATEGORY_ID != 31  AND CATEGORY_ID != 32 AND CATEGORY_ID != 33 AND CATEGORY_ID != 34 AND CATEGORY_ID != 35 AND CATEGORY_ID != 24 AND CATEGORY_ID != 25 AND CATEGORY_ID != 26 AND CATEGORY_ID != 12 AND CATEGORY_ID != 13  AND CATEGORY_ID != 23 AND CATEGORY_ID != 22 AND CATEGORY_ID != 21 AND CATEGORY_ID != 19 AND CATEGORY_ID != 20 AND CATEGORY_ID != 14 AND CATEGORY_ID != 15 AND CATEGORY_ID != 16 AND CATEGORY_ID != 17 AND CATEGORY_ID != 18" )->order ( array (
				"ID DESC" 
		) )->limit ( 10, 0 );
		return $this->fetchAll ( $select )->toArray ();
	}
	public function topNews() {
		$select = $this->select ()->from ( "news", array (
				"id",
				"TITLE" 
		) )->where ( "CATEGORY_ID != 27 AND CATEGORY_ID != 28 AND CATEGORY_ID != 29 AND CATEGORY_ID != 30 AND CATEGORY_ID != 31  AND CATEGORY_ID != 32 AND CATEGORY_ID != 33 AND CATEGORY_ID != 34 AND CATEGORY_ID != 35 AND CATEGORY_ID != 24 AND CATEGORY_ID != 25 AND CATEGORY_ID != 26 AND CATEGORY_ID != 12 AND CATEGORY_ID != 13  AND CATEGORY_ID != 23 AND CATEGORY_ID != 22 AND CATEGORY_ID != 21 AND CATEGORY_ID != 19 AND CATEGORY_ID != 20 AND CATEGORY_ID != 14 AND CATEGORY_ID != 15 AND CATEGORY_ID != 16 AND CATEGORY_ID != 17 AND CATEGORY_ID != 18" )->order ( "VIEW DESC" )->limit ( 9 );
		return $this->fetchAll ( $select )->toArray ();
	}
	public function topKnow($cate_id) {
		return $this->fetchAll ( "CATEGORY_ID = $cate_id AND STATUS = 1", "VIEW DESC", 9 )->toArray ();
	}
	public function topRecrui($cate_id) {
		return $this->fetchAll ( "CATEGORY_ID = $cate_id AND STATUS = 1", "VIEW DESC", 5 )->toArray ();
	}
	public function maxView() {
		return $max = $this->fetchRow ( $this->select ()->from ( $this->_name, array (
				"Max(VIEW) as m" 
		) ) )->toArray ();
	}
	public function greatView() {
		$condition = $this->maxView ();
		$select = $this->select ()->from ( "news", array (
				"id",
				"TITLE",
				"IMG" 
		) )->where ( "VIEW = " . $condition ["m"] );
		return $this->fetchRow ( $select )->toArray ();
	}
	public function newNews() {
		$select = $this->select ()->from ( "news", array (
				"id",
				"TITLE" 
		) )->where ( "CATEGORY_ID != 27 AND CATEGORY_ID != 29 AND CATEGORY_ID != 31  AND CATEGORY_ID != 32 AND CATEGORY_ID != 33 AND CATEGORY_ID != 34 AND CATEGORY_ID != 35 AND CATEGORY_ID != 24 AND CATEGORY_ID != 25 AND CATEGORY_ID != 26 AND CATEGORY_ID != 12 AND CATEGORY_ID != 13  AND CATEGORY_ID != 23 AND CATEGORY_ID != 22 AND CATEGORY_ID != 21 AND CATEGORY_ID != 19 AND CATEGORY_ID != 20 AND CATEGORY_ID != 14 AND CATEGORY_ID != 15 AND CATEGORY_ID != 16 AND CATEGORY_ID != 17 AND CATEGORY_ID != 18" )->order ( "ID DESC" )->limit ( 10, 0 );
		return $this->fetchAll ( $select )->toArray ();
	}
	public function getNewsMaxId($cate_id) {
		return $max = $this->fetchRow ( $this->select ()->from ( $this->_name, array (
				"Max(ID) as m" 
		) )->where ( "CATEGORY_ID = $cate_id" ) )->toArray ();
	}
}

